<template>
  <div>
    <div class="post-contents" v-if="postsdata.images.length >= 3">
      <div @click="goDetail(postsdata.id)">
        <p class="title">{{ postsdata.title }}</p>
        <p class="content" v-html="postsdata.summary"></p>
        <div class="imgs">
          <img :src="postsdata.images[0]" alt />
          <img :src="postsdata.images[1]" alt />
          <img :src="postsdata.images[2]" alt />
        </div>
      </div>
      <div class="postInfo">
        <div class="info-left">
          <span class="city">
            <i class="iconfont">&#xe611;</i>
            {{ postsdata.cityName }}
          </span>
          <div class="userinfo">
            <img
              class="headImg"
              :src="$axios.defaults.baseURL + postsdata.account.defaultAvatar"
              alt
            />
            <span class="name">{{ postsdata.account.nickname }}</span>
          </div>
          <span class="num">
            <i class="iconfont">&#xe640;</i>
            {{ postsdata.watch }}
          </span>
        </div>
        <div class="info-right" @click="dianzhan(postsdata.id)">
          <b>{{ postsdata.like }}</b>
          <img src="../../static/images/ding.png" alt="" />
        </div>
      </div>
    </div>
    <div class="post-content" v-if="postsdata.images.length < 3">
      <div @click="goDetail(postsdata.id)">
        <img
          v-if="postsdata.images.length == 0"
          src="../../static/images/img.jpg"
          alt
        />
        <img v-else :src="postsdata.images[0]" alt />
      </div>
      <div class="contentBox">
        <div @click="goDetail(postsdata.id)">
          <p class="title">{{ postsdata.title }}</p>
          <p class="content" v-html="postsdata.summary"></p>
        </div>
        <div class="postInfo">
          <div class="info-left">
            <span class="city">
              <i class="iconfont">&#xe611;</i>
              {{ postsdata.cityName }}
            </span>
            <div class="userinfo">
              <img
                class="headImg"
                :src="$axios.defaults.baseURL + postsdata.account.defaultAvatar"
                alt
              />

              <span class="name">{{ postsdata.account.nickname }}</span>
            </div>
            <span class="num">
              <i class="iconfont">&#xe640;</i>
              {{ postsdata.watch || 0 }}
            </span>
          </div>
          <div class="info-right" @click="dianzhan(postsdata.id)">
            <b>{{ postsdata.like || 0 }}</b>
            <!-- <b v-else>0</b> -->
            <img src="../../static/images/ding.png" alt="" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: ["postsdata"],

  // mounted() {
  //   console.log("文章模块", this.postsdata);
  // }
  methods: {
    goDetail(id) {
      // console.log(id);
      this.$router.push({
        path: "/post/detail?id=" + id
      });
    },
    dianzhan(id) {
      this.$axios({
        url: "/posts/like?id=" + id,
        headers: {
          Authorization: "Bearer " + this.$store.state.user.userInfo.token
        }
      }).then(res => {
        // console.log(res);
        if (res.data.message == "点赞成功") {
          this.postsdata.like += 1;
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.post-contents {
  color: #333;
  border-bottom: 1px solid #ccc;
  cursor: pointer;
  .title {
    padding: 10px 0;
    font-size: 18px;
    &:hover {
      color: orange;
    }
  }
  .content {
    margin: 10px 0;
    font-size: 13px;
    line-height: 20px;
    color: #333;
    text-overflow: -o-ellipsis-lastline;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
  }
  .imgs {
    img {
      width: 220px;
      height: 150px;
      object-fit: cover;
    }
  }
  .city,
  .num {
    font-size: 12px;
    .iconfont {
      font-size: 14px;
    }
  }
  .postInfo {
    height: 40px;
    margin: 10px;
    color: #666;
    .info-left {
      float: left;
      .userinfo {
        font-size: 12px;
        display: inline-block;
        margin: 0 8px;
        .headImg {
          width: 16px;
          height: 16px;
          border-radius: 8px;
        }
        .name {
          color: orange;
        }
      }
    }
    .info-right {
      display: inline-block;
      float: right;
      color: orange;
      margin-right: 10px;
      img {
        float: right;
        margin-left: 3px;
        width: 35px;
        height: 26px;
      }
    }
  }
}
.post-content {
  padding-top: 25px;
  border-bottom: 1px solid #ccc;
  cursor: pointer;
  img {
    float: left;
    margin-right: 10px;
    width: 220px;
    height: 150px;
    vertical-align: middle;
  }
  .contentBox {
    color: #333;
    .title {
      font-size: 18px;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 3;
      overflow: hidden;
      &:hover {
        color: orange;
      }
    }
    .content {
      margin-top: 15px;
      font-size: 13px;
      line-height: 20px;
      height: 60px;
      color: #333;
      text-overflow: -o-ellipsis-lastline;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
    }
    .postInfo {
      height: 40px;
      margin: 10px;
      color: #666;
      .info-left {
        float: left;
        .city,
        .num {
          font-size: 12px;
          .iconfont {
            font-size: 14px;
          }
        }
        .userinfo {
          font-size: 12px;
          display: inline-block;
          margin: 0 8px;
          .headImg {
            width: 16px;
            height: 16px;
            border-radius: 8px;
          }
          .name {
            color: orange;
          }
        }
      }
      .info-right {
        display: inline-block;
        float: right;
        color: orange;
        img {
          float: right;
          margin-left: 3px;
          width: 35px;
          height: 26px;
        }
      }
    }
  }
}
</style>
